//
//  NewCollectionViewCell.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/20.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "NewCollectionViewCell.h"

@interface NewCollectionViewCell ()
@property (nonatomic, retain) BaseImageView *imgPhoto; /**< 图标 */
@property (nonatomic, retain) BaseLabel *labelname; /**< 图标名字 */
@property (nonatomic, retain) BaseLabel *labelContent; /**< 内容 */
@property (nonatomic, retain) BaseLabel *labelPrice; /**< 价钱 */
@property (nonatomic, retain) BaseLabel *labelOldPrice; /**< 原价 */
@end

@implementation NewCollectionViewCell


#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    [_imgPhoto release];
    [_labelname release];
    [_labelContent release];
    [_labelPrice release];
    [_labelOldPrice release];
    [_model release];
    [super dealloc];
    
}

#pragma mark - ------------ 初始化控件 ---------------
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
     //   self.contentView.backgroundColor = [UIColor redColor];
        [self createSubViews];

    }
    return self;
}

#pragma mark - ------------ 设置大小 ---------------
- (void)createSubViews
{
    
    self.imgPhoto = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgPhoto];
    _imgPhoto.contentMode = UIViewContentModeScaleAspectFill;
    _imgPhoto.clipsToBounds = YES;
    
    self.labelname = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelname];
    
    
    self.labelContent = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelContent];
    
    self.labelPrice = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelPrice];
    
    
    self.labelOldPrice = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelOldPrice];
    
    
    [_imgPhoto release];
    [_labelname release];
    [_labelContent release];
    [_labelPrice release];
    [_labelOldPrice release];
    
//    _imgPhoto.backgroundColor = [UIColor orangeColor];
//    _labelname.backgroundColor = [UIColor yellowColor];
//    _labelContent.backgroundColor = [UIColor grayColor];
//    _labelPrice.backgroundColor = [UIColor blackColor];
//    _labelOldPrice.backgroundColor = [UIColor yellowColor];
}

#pragma mark - ------------ 约束 赋大小 ---------------
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    
    
    _imgPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    _labelname.translatesAutoresizingMaskIntoConstraints = NO;
    _labelContent.translatesAutoresizingMaskIntoConstraints = NO;
    _labelPrice.translatesAutoresizingMaskIntoConstraints = NO;
    _labelOldPrice.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    //距离content 上左 右 为0 下距离label 5
    [_imgPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(0);
        
        make.right.mas_equalTo(self.contentView).offset(0);

        make.bottom.mas_equalTo(_labelname.mas_top).offset(-5);
    }];

    //距离左右下 为 0 高度
    [_labelname mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.right.mas_equalTo(_imgPhoto);
        make.height.mas_equalTo(30);
        make.bottom.mas_equalTo(_labelContent.mas_top).offset(-5);
    }];
    
    [_labelContent mas_makeConstraints:^(MASConstraintMaker *make) {
        
       make.left.right.mas_equalTo(_imgPhoto);
        make.height.mas_equalTo(_labelname);
        make.bottom.mas_equalTo(_labelPrice.mas_top).offset(-5);
    }];
    
    
    
    [_labelPrice mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.contentView).offset(10);
        make.height.mas_equalTo(30);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
        make.width.mas_equalTo(80);
        
    }];
    
    [_labelOldPrice mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(_labelContent.mas_bottom).offset(5);
        make.left.mas_equalTo(_labelPrice.mas_right).offset(0);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
        make.right.mas_equalTo(self.contentView).offset(-10);
        
    }];
    
}


#pragma mark - ------------ model赋值 ---------------
- (void)setModel:(EnjoyModel *)model
{

    if (_model != model) {
        [_model release];
        _model = [model retain];
        
    }
    
    [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:model.product_image] placeholderImage:PLACEHOLDER];
    
    //主标题
    _labelname.text = model.short_name;
    _labelname.font = [UIFont systemFontOfSize:16];
/** 夜间 */
    _labelname.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);

    //副标题
    _labelContent.text = model.short_description;
    _labelContent.font = [UIFont systemFontOfSize:14];
    _labelContent.textColor = [UIColor lightGrayColor];
    
    //价钱
    
    NSInteger i = model.price.integerValue / 100;
    
    NSString *str = [NSString stringWithFormat:@"%ld", (long)i];
    
    
    _labelPrice.text = [NSString stringWithFormat:@"%@ %@/%@",str,@"元", model.entity_name];
    _labelPrice.textColor = [UIColor redColor];
    _labelPrice.font = [UIFont systemFontOfSize:16];
    
    //原价
    
    if (model.original_price) {
        
        NSString *str = [NSString stringWithFormat:@"%@ %@", @"¥", [model.original_price.stringValue substringToIndex:2]];
       
        //添加删除线  NSUnderlineStyleAttributeName下划线
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc]initWithString:str];
        
        [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleDouble | NSUnderlinePatternSolid) range:NSMakeRange(0, [str length])];
        
        _labelOldPrice.attributedText = attri;
        _labelOldPrice.textColor = [UIColor lightGrayColor];
    }
    
}















/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
